package com.rykj.qxj.server.services.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.rykj.qxj.server.common.exception.ApiAbilityStaticsticsIsNullException;
import com.rykj.qxj.server.common.exception.AppIdIsNullException;
import com.rykj.qxj.server.common.exception.ApplyIsNotExistException;
import com.rykj.qxj.server.dao.ApiManageMapper;
import com.rykj.qxj.server.dao.ApiWarnInfoMapper;
import com.rykj.qxj.server.dao.ApplyManageMapper;
import com.rykj.qxj.server.model.entity.ApiManage;
import com.rykj.qxj.server.model.entity.ApiWarnInfo;
import com.rykj.qxj.server.model.entity.Apply;
import com.rykj.qxj.server.services.IApiWarnInfoService;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class ApiWarnInfoServiceImpl implements IApiWarnInfoService {
	
	@Autowired
	private ApiWarnInfoMapper apiWarnInfoMapper;
	
	@Autowired
	private ApplyManageMapper applyManageMapper;
	
	@Autowired
	private ApiManageMapper apiManageMapper;
	

	@Override
	public void saveApiWarnInfo(ApiWarnInfo apiWarnInfo) throws ApiAbilityStaticsticsIsNullException, AppIdIsNullException, ApplyIsNotExistException {
		if(apiWarnInfo ==null) {
			throw new ApiAbilityStaticsticsIsNullException("保存信息不能为空");
			
		}
		if(StringUtil.isEmpty(apiWarnInfo.getAppKey())) {
			throw new AppIdIsNullException("应用的appId不能为空");
			
		}
		
		Apply apply = applyManageMapper.selectApplyByApplyId(apiWarnInfo.getAppKey());
		if(apply ==null) {
			throw new ApplyIsNotExistException("此appId没有对应的应用,请检查");
			
		}
		apiWarnInfoMapper.insert(apiWarnInfo);

	}

	@Override
	public PageInfo selectPageApiWarnInfoByappKey(Integer pageNum, Integer pageSize, String appKey) throws ParseException {
		PageHelper.startPage(pageNum, pageSize);
		List<ApiWarnInfo> apiWarnInfoList = apiWarnInfoMapper.selectListApiWarnInfoByappKey(appKey);
		if(apiWarnInfoList!=null) {
			for (ApiWarnInfo apiWarnInfo : apiWarnInfoList) {
				
     			SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy:hh:mm:ss", Locale.ENGLISH);
     			Date date = formatter.parse(apiWarnInfo.getWarnTime());
     			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     			apiWarnInfo.setWarnTime(format.format(date));
     			if(apiWarnInfo.getWarnCode().equals("500")) {
     				String desc="the Exception Code is->"+apiWarnInfo.getWarnCode();
     				apiWarnInfo.setWarnDesc(desc);
     				apiWarnInfo.setWarnLevel("一级");
     				
     			}else if(apiWarnInfo.getWarnCode().equals("400")) {
     				String desc="the Exception Code is->"+apiWarnInfo.getWarnCode();
     				apiWarnInfo.setWarnDesc(desc);
     				apiWarnInfo.setWarnLevel("二级");
     			}else {
     				String desc="the Exception Code is->"+apiWarnInfo.getWarnCode();
         			apiWarnInfo.setWarnDesc(desc);
     				apiWarnInfo.setWarnLevel("三级");
     				
     			}
 				apiWarnInfoMapper.updateByPrimaryKeySelective(apiWarnInfo);
			}
			
		}
		PageInfo page=new PageInfo<>(apiWarnInfoList);
		
		return page;
	}

}
